import allure
from selenium import webdriver
import time
from selenium.webdriver.common.by import By




class Tools:

    def __init__(self, driver):
        # driver: 浏览器，只在你传递进来的浏览器操作
        self.driver = driver
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)

    def visit(self, url):
        # url: 网页地址
        self.driver.get(url)
        time.sleep(0.5)

    def by_css(self, css):
        """根据css定位，返回定位的元素"""
        return self.driver.find_element(By.CSS_SELECTOR, css)

    def by_link(self, link):
        """根据Link_Text 定位，返回定位的元素"""
        return self.driver.find_element(By.PARTIAL_LINK_TEXT, link)

    def quit(self):
        self.driver.quit()

    def img(self):
        title = time.strftime('%Y_%m_#d_%H_%M_%S')
        # 截图、附加在测试报告中
        allure.attach(self.driver.get_screenshot_as_png(),title,allure.attachment_type.PNG)